<template>
  <uni-popup ref="repairPopup" type="center">
    <view class="repair-popup-content">
      <text class="repair-popup-title">再次返修</text>
      <textarea 
        class="repair-reason-input" 
        v-model="repairReason" 
        placeholder="请输入返修原因描述" 
        maxlength="400"
      ></textarea>
      <view class="repair-popup-btn-group">
        <button class="repair-popup-cancel-btn" @click="closeRepairPopup">取消</button>
        <button class="repair-popup-confirm-btn" @click="confirmRepair">确定</button>
      </view>
    </view>
  </uni-popup>
</template>

<script>
// 引入返修接口（必须引入，组件内需要直接调用）
import { repairTicket } from '../life/repairDetail/repairDetail.js'; // 路径根据实际位置调整

export default {
  // 接收父组件传递的工单编号
  props: {
    orderNumber: {
      type: String,
      required: true, // 必须传递，否则报错提醒
      default: ''
    }
  },
  data() {
    return {
      repairReason: ''
    };
  },
  methods: {
    // 打开弹窗
    open() {
      if (this.$refs.repairPopup) {
        this.$refs.repairPopup.open();
        this.repairReason = ''; // 清空历史输入
      }
    },
    // 关闭弹窗
    closeRepairPopup() {
      this.$refs.repairPopup.close();
    },
    // 组件内部执行返修逻辑（使用父组件传递的 orderNumber）
    async confirmRepair() {
      if (!this.repairReason.trim()) {
        uni.showToast({ title: '请输入返修原因', icon: 'none' });
        return;
      }
      try {
        uni.showLoading({ title: '提交中...' });
		console.log("返修的原因",this.repairReason)
        // 调用返修接口，传入工单编号和返修原因
        const res = await repairTicket(this.orderNumber, this.repairReason);
        if (res.success) {
          uni.showToast({ title: '再次报修成功', icon: 'success' });
          this.closeRepairPopup();
          // 延迟返回上一页
          setTimeout(() => uni.navigateBack({ delta: 1 }), 1500);
        } else {
          uni.showToast({ title: res.msg || '返修失败', icon: 'none' });
        }
      } catch (error) {
        console.error('返修接口调用失败:', error);
        uni.showToast({ title: '网络异常，请重试', icon: 'none' });
      } finally {
        uni.hideLoading();
      }
    }
  }
};
</script>

<style scoped>
.repair-popup-content {
  width: 75vw;
  max-width: 2000px;
  min-height: 280px;
  max-height: 35vh;
  background-color: #fff;
  border-radius: 16px;
  padding: 16px;
  box-sizing: border-box;
  margin: auto;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

.repair-popup-title {
  display: flex;
  justify-content: center;
  font-size: 18px;
  font-weight: 600;
  color: #333;
  text-align: center;
  margin-bottom: 16px;
}

.repair-reason-input {
  width: 100%;
  min-height: 60px;
  padding: 10px;
  border: 1px solid #e5e5e5;
  border-radius: 8px;
  font-size: 14px;
  color: #666;
  line-height: 1.4;
  box-sizing: border-box;
  resize: none;
}

.repair-popup-btn-group {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-top: 18px;
}

.repair-popup-cancel-btn,
.repair-popup-confirm-btn {
  flex: 1;
  height: 40px;
  line-height: 40px;
  text-align: center;
  border-radius: 24px;
  font-size: 15px;
  transition: all 0.3s ease;
}

.repair-popup-cancel-btn {
  background-color: #f8f8f8;
  color: #999;
  border: 1px solid #e5e5e5;
}

.repair-popup-cancel-btn:active {
  background-color: #f0f0f0;
}

.repair-popup-confirm-btn {
  background-color: #1677ff;
  color: #fff;
  border: none;
}

.repair-popup-confirm-btn:active {
  background-color: #0958d9;
}
</style>